const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'community_service_platform',
  port: 3306
};

// 检查技能数据
async function checkSkillsData() {
  try {
    // 创建连接
    const connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 检查技能表是否存在
    const [tables] = await connection.execute('SHOW TABLES');
    const tableNames = tables.map(table => Object.values(table)[0]);
    console.log('存在的表:', tableNames);

    // 检查表结构 - 优先使用shared_skills表
    if (tableNames.includes('shared_skills')) {
      const [columns] = await connection.execute('DESCRIBE shared_skills');
      console.log('shared_skills表结构:', columns);

      // 查询技能数据
      const [skills] = await connection.execute('SELECT * FROM shared_skills WHERE status = "approved"');
      console.log('已批准的技能数据:', skills);

      // 如果没有数据，插入测试数据
      if (skills.length === 0) {
        console.log('没有找到已发布的技能数据，正在插入测试数据...');
        const insertSql = `
          INSERT INTO shared_skills (user_id, title, category, description, status, hourly_rate, created_at)
          VALUES 
            (1, '电脑维修', '维修服务', '提供电脑软硬件维修服务，经验丰富。', 'approved', 50.00, NOW()),
            (1, '英语辅导', '教育培训', '英语专业毕业，可提供小学到高中英语辅导。', 'approved', 80.00, NOW()),
            (1, '手工编织', '生活服务', '擅长毛衣、围巾等编织，可教授编织技巧。', 'approved', 40.00, NOW())
        `;
        await connection.execute(insertSql);
        console.log('测试数据插入成功');

        // 再次查询
        const [newSkills] = await connection.execute('SELECT * FROM shared_skills WHERE status = "approved"');
        console.log('插入后的技能数据:', newSkills);
      }
    } else if (tableNames.includes('skills')) {
      const [columns] = await connection.execute('DESCRIBE shared_skills');
      console.log('shared_skills表结构:', columns);

      // 查询技能数据
      const [skills] = await connection.execute('SELECT * FROM shared_skills WHERE status = "approved"');
      console.log('已批准的技能数据:', skills);
    } else {
      console.log('未找到skills或shared_skills表');
    }

    // 关闭连接
    await connection.end();
  } catch (error) {
    console.error('❌ 数据库操作失败:', error.message);
  }
}

// 执行检查
checkSkillsData();